package com.heima.common.util;

import com.heima.utils.common.AppJwtUtil;
import org.springframework.http.StreamingHttpOutputMessage;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author mgw
 * @create 2021/10/5 22:16
 */
public class RequestContextUtil {
    public static String getHeaders(String headerName){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        return request.getHeader(headerName);
    }
    public static Map<String,Object> getToken(){
        String token = getHeaders("token");
        Map<String, Object> claimsBody = AppJwtUtil.getClaimsBody(token);
        return claimsBody;
    }
    public static <T> T getNode(String key){
        Map<String, Object> token = getToken();
       return (T) token.get(key);
    }
}
